package icegps.com.pingdj.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import icegps.com.pingdj.R;
import icegps.com.pingdj.bean.DeviceStatus;
import icegps.com.pingdj.utils.UnitUtils;

/**
 * Created by 111 on 2018/3/23.
 */

public class UnitTextView extends android.support.v7.widget.AppCompatTextView {


    public UnitTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.unitTextView);
        if (ta != null) {
            int unitType = ta.getInteger(R.styleable.unitTextView_unit_type, 1);
            refUnit(unitType);
        }
    }

    public void refUnit(int unitType) {
        int unitIndex = 0;
        DeviceStatus settingConfig = DeviceStatus.getDeviceStatus();
        switch (unitType) {
            case UnitUtils.AREA:
                unitIndex = settingConfig.getArea();
                break;
            case UnitUtils.HEIGHT:
                unitIndex = settingConfig.getHeight();
                break;
            case UnitUtils.LENGTH:
                unitIndex = settingConfig.getLength();
                break;
            case UnitUtils.SPEED:
                unitIndex = settingConfig.getSpeed();
                break;
            case UnitUtils.VOLUME:
                unitIndex = settingConfig.getVolume();
                break;
            case UnitUtils.DEGREES:
                setText("°");
                return;
            case UnitUtils.PERCENT:
                setText("‰");
                return;
        }
        String unit = UnitUtils.index2Value(unitType, unitIndex);
        setText(unit);
    }
}
